/**
 * 系统信息相关工具函数
 * 主要用于获取小程序系统信息，包括胶囊按钮位置等
 */

/**
 * 获取小程序系统信息和胶囊按钮信息
 * @returns {Object} 系统信息对象
 */
export function getSystemInfo() {
	try {
		const systemInfo = uni.getSystemInfoSync();
		const menuButtonInfo = uni.getMenuButtonBoundingClientRect();
		
		// 计算状态栏高度
		const statusBarHeight = systemInfo.statusBarHeight || 0;
		
		// 计算胶囊按钮相关尺寸
		const menuButtonHeight = menuButtonInfo.height || 32;
		const menuButtonTop = menuButtonInfo.top || statusBarHeight;
		const menuButtonWidth = menuButtonInfo.width || 87;
		const menuButtonRight = menuButtonInfo.right || systemInfo.windowWidth;
		
		// 计算导航栏高度（状态栏高度 + 胶囊按钮高度 + 胶囊按钮与状态栏的间距 * 2）
		const navigationBarHeight = menuButtonTop + menuButtonHeight + (menuButtonTop - statusBarHeight);
		
		// 计算胶囊按钮与顶部屏幕的距离
		const capsuleToTopDistance = menuButtonTop;
		
		// 计算胶囊按钮与状态栏的间距
		const capsuleToStatusBarGap = menuButtonTop - statusBarHeight;
		
		return {
			// 系统基本信息
			platform: systemInfo.platform,
			system: systemInfo.system,
			model: systemInfo.model,
			brand: systemInfo.brand,
			version: systemInfo.version,
			SDKVersion: systemInfo.SDKVersion,
			
			// 屏幕尺寸信息
			screenWidth: systemInfo.screenWidth,
			screenHeight: systemInfo.screenHeight,
			windowWidth: systemInfo.windowWidth,
			windowHeight: systemInfo.windowHeight,
			pixelRatio: systemInfo.pixelRatio,
			
			// 状态栏信息
			statusBarHeight: statusBarHeight,
			
			// 胶囊按钮信息
			menuButton: {
				width: menuButtonWidth,
				height: menuButtonHeight,
				top: menuButtonTop,
				right: menuButtonRight,
				bottom: menuButtonInfo.bottom || (menuButtonTop + menuButtonHeight),
				left: menuButtonInfo.left || (menuButtonRight - menuButtonWidth)
			},
			
			// 计算得出的关键尺寸
			navigationBarHeight: navigationBarHeight,
			capsuleToTopDistance: capsuleToTopDistance,
			capsuleToStatusBarGap: capsuleToStatusBarGap,
			
			// 安全区域信息（如果支持）
			safeArea: systemInfo.safeArea || null,
			safeAreaInsets: systemInfo.safeAreaInsets || null
		};
	} catch (error) {
		console.error('获取系统信息失败:', error);
		
		// 返回默认值
		return {
			platform: 'unknown',
			statusBarHeight: 20,
			navigationBarHeight: 44,
			capsuleToTopDistance: 20,
			capsuleToStatusBarGap: 0,
			menuButton: {
				width: 87,
				height: 32,
				top: 20,
				right: 375,
				bottom: 52,
				left: 288
			}
		};
	}
}

/**
 * 获取自定义导航栏样式
 * @returns {Object} 导航栏样式对象
 */
export function getCustomNavigationStyle() {
	const systemInfo = getSystemInfo();
	
	return {
		// 状态栏高度
		statusBarHeight: systemInfo.statusBarHeight + 'px',
		
		// 导航栏总高度
		navigationBarHeight: systemInfo.navigationBarHeight + 'px',
		
		// 胶囊按钮高度（用于设置自定义导航栏内容高度）
		capsuleHeight: systemInfo.menuButton.height + 'px',
		
		// 胶囊按钮与状态栏的间距
		capsuleGap: systemInfo.capsuleToStatusBarGap + 'px',
		
		// 胶囊按钮距离右侧的距离
		capsuleRight: (systemInfo.windowWidth - systemInfo.menuButton.right) + 'px',
		
		// 胶囊按钮宽度
		capsuleWidth: systemInfo.menuButton.width + 'px'
	};
}

/**
 * 判断是否为刘海屏
 * @returns {boolean}
 */
export function isNotchScreen() {
	const systemInfo = getSystemInfo();
	
	// 通过状态栏高度判断是否为刘海屏
	// iPhone X 及以上机型状态栏高度通常大于 20
	if (systemInfo.platform === 'ios') {
		return systemInfo.statusBarHeight > 20;
	}
	
	// Android 设备判断
	if (systemInfo.platform === 'android') {
		return systemInfo.statusBarHeight > 24;
	}
	
	return false;
}

/**
 * 获取底部安全区域高度
 * @returns {number}
 */
export function getBottomSafeAreaHeight() {
	const systemInfo = getSystemInfo();
	
	if (systemInfo.safeAreaInsets && systemInfo.safeAreaInsets.bottom) {
		return systemInfo.safeAreaInsets.bottom;
	}
	
	// 对于没有 safeAreaInsets 的情况，通过机型判断
	if (isNotchScreen()) {
		return 34; // iPhone X 系列底部安全区域高度
	}
	
	return 0;
}

/**
 * 获取页面可用高度（去除导航栏和底部安全区域）
 * @returns {number}
 */
export function getAvailableHeight() {
	const systemInfo = getSystemInfo();
	const bottomSafeHeight = getBottomSafeAreaHeight();
	
	return systemInfo.windowHeight - systemInfo.navigationBarHeight - bottomSafeHeight;
}

/**
 * 缓存系统信息（避免重复计算）
 */
let cachedSystemInfo = null;

/**
 * 获取缓存的系统信息
 * @param {boolean} forceRefresh - 是否强制刷新缓存
 * @returns {Object}
 */
export function getCachedSystemInfo(forceRefresh = false) {
	if (!cachedSystemInfo || forceRefresh) {
		cachedSystemInfo = getSystemInfo();
	}
	return cachedSystemInfo;
}

/**
 * 清除系统信息缓存
 */
export function clearSystemInfoCache() {
	cachedSystemInfo = null;
}
